// MongoDB 初始化脚本
db = db.getSiblingDB('devops-smart-bot');

// 创建应用用户
db.createUser({
  user: 'devops',
  pwd: 'devops123',
  roles: [
    {
      role: 'readWrite',
      db: 'devops-smart-bot'
    }
  ]
});

// 创建基础集合
db.createCollection('users');
db.createCollection('roles');
db.createCollection('permissions');
db.createCollection('alerts');
db.createCollection('tickets');
db.createCollection('knowledge');

// 插入默认角色
db.roles.insertMany([
  {
    _id: ObjectId(),
    name: 'admin',
    displayName: '系统管理员',
    description: '拥有系统所有权限',
    permissions: ['all'],
    isSystemRole: true,
    createdAt: new Date(),
    updatedAt: new Date()
  },
  {
    _id: ObjectId(),
    name: 'ops',
    displayName: '运维工程师',
    description: '负责告警和工单处理',
    permissions: ['alerts:read', 'alerts:update', 'tickets:read', 'tickets:create', 'tickets:update'],
    isSystemRole: true,
    createdAt: new Date(),
    updatedAt: new Date()
  },
  {
    _id: ObjectId(),
    name: 'developer',
    displayName: '开发工程师',
    description: '开发和技术支持',
    permissions: ['alerts:read', 'tickets:read', 'knowledge:read'],
    isSystemRole: true,
    createdAt: new Date(),
    updatedAt: new Date()
  }
]);

// 插入默认管理员用户
db.users.insertOne({
  _id: ObjectId(),
  username: 'admin',
  email: 'admin@example.com',
  // 密码: admin123 (需要在应用中进行哈希处理)
  password: '$2a$10$rLs7lX8GRHyQqxLbgpVjOeLnQl7X8GRHyQqxLbgpVjOeLnQl7X8G',
  department: 'IT',
  roles: ['admin'],
  isActive: true,
  createdAt: new Date(),
  updatedAt: new Date()
});

print('MongoDB 初始化完成');
